package design.graduation.novelwebsite.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import design.graduation.novelwebsite.constant.Constant;
import design.graduation.novelwebsite.model.dto.ResultJson;
import design.graduation.novelwebsite.model.entity.IncludeBook;
import design.graduation.novelwebsite.service.IncludeBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 洪少尉
 * @Date: 2020/3/27 21:32
 * @Description:
 */
@RestController
@CrossOrigin
@RequestMapping("recommend")
public class RecommendationController {

	@Autowired
	private IncludeBookService includeBookService;
	/**
	 * 最新小说
	 */
	@RequestMapping("getNewest")
	public ResultJson getNewest(){
		return getResultJson(IncludeBook.COL_CREATE_TIME);
	}
	/**
	 * 总点击榜
	 */
	@RequestMapping("getTotalClicks")
	public ResultJson getTotalClicks(){
		return getResultJson(IncludeBook.COL_TOTAL_CLICKS);
	}
	/**
	 * 月点击榜
	 */
	@RequestMapping("getMonthClicks")
	public ResultJson getMonthClicks(){
		return getResultJson(IncludeBook.COL_MONTH_CLICKS);
	}
	/**
	 *  随机推荐
	 */
	@RequestMapping("getRandom")
	public ResultJson getRandom(){
		QueryWrapper<IncludeBook> queryWrapper = new QueryWrapper<IncludeBook>();
		queryWrapper.eq(IncludeBook.COL_STATUS, Constant.STATUS_TRUE)
				.last("order by rand() limit 8");
		List<IncludeBook> books = this.includeBookService.list(queryWrapper);
		if (books.isEmpty()){
			return new ResultJson(Constant.ERROR_CODE,"查询失败",books);
		}
		return new ResultJson(Constant.SUCCESS_CODE,"查询成功",books);
	}



	public ResultJson getResultJson(String databaseField){
		QueryWrapper<IncludeBook> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(IncludeBook.COL_STATUS, Constant.STATUS_TRUE)
				.orderByDesc(databaseField)
				.last("limit 8");
		List<IncludeBook> books = this.includeBookService.list(queryWrapper);
		if (books.isEmpty()){
			return new ResultJson(Constant.ERROR_CODE,"查询失败",books);
		}
		return new ResultJson(Constant.SUCCESS_CODE,"查询成功",books);
	}
}
